package com.kun.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

public class ConnectionUtil {
	
	private static String URL = null;
	private static String USERNAME = null;
	private static String PASSWORD = null;
	
	private static Connection conn = null;
	static {
		ResourceBundle bundle = ResourceBundle.getBundle("config");
		URL = bundle.getString("url");
		USERNAME = bundle.getString("username");
		PASSWORD = bundle.getString("password");
		String driver = bundle.getString("driver");
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	
	public  Connection getConn(){
		synchronized (ConnectionUtil.class) {
			if(conn == null){
				try {
					conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
					System.out.println("conn========="+conn);
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			return conn;
		}
	}
	
	/**
	 *关闭连接 
	 * @param rs
	 * @param ps
	 * @param conn
	 */
	public static void closeConn(ResultSet rs,PreparedStatement ps,Connection conn){
		if(rs != null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(ps != null){
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(conn != null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 关闭连接
	 * @param rs
	 * @param st
	 * @param conn
	 */
	public static void closeConn(ResultSet rs,Statement st,Connection conn){
		if(rs != null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(st != null){
			try {
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(conn != null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
